#pragma once
#include "point.h"
#include <iostream>

using std::cout;
using std::endl;

class Map{

public:

	Map(){ 
		p = NULL;	
	}
	~Map(){}
	int initMap(int x, int y){
		if( x <= 0 || y <= 0 ){
			return -1;
		}
		xSize = x;
		ySize = y;
		maps = new Point*[x];
		for(int i=0; i < xSize; i++)
			maps[i] = new Point[y];

		
		for(int i=0; i<x; i++){
			for(int j=0; j<y; j++){
				maps[i][j].init(i, j);
			}
		}
	}

	void drawMap(){
		cout<<RESET;
		for(int j=0; j<ySize; j++){
			if( j == 0 ){
				cout<<"   "<<j<<"  ";
				continue;
			}
			if( j < 10 )
				cout<<j<<"  ";
			else
				cout<<j<<" ";
		}
		cout<<endl;
		for(int i=0; i<xSize; i++){
			if( i < 10 )
				cout<<i<<"  ";
			else
				cout<<i<<" ";
			
			for(int j=0; j<ySize; j++){
				if( maps[i][j].getType() == 0 ){
					cout<<"*  ";
				}else if( maps[i][j].getType() == 1 ) {
					cout<<WHITE<<maps[i][j].getValue()<<RESET<<"  ";
				}else if( maps[i][j].getType() == 2 ) {
					cout<<RED<<maps[i][j].getValue()<<RESET<<"  ";
				}
				//maps[i][j].setType(1);
			}
			cout<<endl;
		}
		cout<<RESET;
	}

	int Random(int num){
		if( num <= 0 ){
			return -1;
		}
	
		if( p != NULL){
			delete[] p;
		}
		for(int i=0; i<num; ){
			int xR = random()%xSize;
			int yR = random()%ySize;
			if( maps[xR][yR].getValue() == 0 ){
				maps[xR][yR].setValue(random()%10);
				maps[xR][yR].setType(i%2+1);
				i++;
				continue;
			}
		}
		
	}

	Point* getPoint(int x, int y){
		if( x < 0 || x > xSize || y < 0 || y > ySize ) {
			return NULL;
		}
		return &maps[x][y];
	}

private:
	Point **maps;
	int xSize;
	int ySize;
	Point *p;	

};

